package com.rankeiot.core.excel;

import com.rankeiot.core.translator.Translator;
import com.rankeiot.core.util.StringUtil;
import jxl.*;
import lombok.SneakyThrows;

import java.io.InputStream;
import java.util.*;
import java.util.function.Consumer;

public class XlsTemplateImporter implements Consumer<Map<String,?>> {
    private int batchSize=500;
    private final InputStream in;
    private final Map<String, Translator> translators;
    private final Map<String,String> columnFieldMapping;
    private List temp = new ArrayList<>();
    private int startRow=0;
    private long currentIndex=0;

    protected BatchImportHandler batchImportHandler;
    public XlsTemplateImporter(InputStream in,Map<String,String> columnFieldMapping){
        this(in,columnFieldMapping,new HashMap<>());
    }
    public XlsTemplateImporter(InputStream in,Map<String,String> columnFieldMapping,Map<String, Translator> translators){
        this.in=in;
        this.columnFieldMapping=columnFieldMapping;
        this.translators=translators;
        if(columnFieldMapping==null||columnFieldMapping.isEmpty()){
            throw new IllegalArgumentException("字段列映射不能为空");
        }
    }

    public XlsTemplateImporter setStartRow(int startRow) {
        this.startRow = startRow;
        return this;
    }

    public XlsTemplateImporter translate(String field, Translator translator){
        translators.put(field,translator);
        return this;
    }
    public XlsTemplateImporter addFieldMapping(String column,String fieldName){
        if(StringUtil.isEmpty(fieldName)){
            columnFieldMapping.remove(column);
        }else{
            columnFieldMapping.put(column,fieldName);
        }
        return this;
    }
    public XlsTemplateImporter setBatchSize(int batchSize) {
        this.batchSize = batchSize;
        return this;
    }

    @SneakyThrows
    protected void load(InputStream in, MapRowHandler th){
        if(columnFieldMapping.isEmpty()){
            throw new IllegalArgumentException("字段列映射不能为空");
        }
        DataRowHandler dh = new DataRowHandler(th);
        Workbook wk=Workbook.getWorkbook(in);
        Sheet sheet= wk.getSheet(0);
        for(int i=startRow;i<sheet.getRows();i++){
            DataRow dr=new DataRow();
            Cell[] cells = sheet.getRow(i);
            for(Cell cell:cells){
               if(cell.getType()== CellType.EMPTY){
                   continue;
               }
               dr.put(cell.getColumn(),cell.getContents());
               dr.setRowNum(cell.getRow());
            }
            if(!dr.isEmpty()){
                th.accept(dr);
            }
        }
        wk.close();
    }
    public void start(BatchImportHandler<Map<String,?>> bh){
        batchImportHandler=bh;
        MapRowHandler th = new MapRowHandler(columnFieldMapping, translators, this);
        th.setStartRow(0);
        load(in,th);
        if(temp.size()>0){
            onBatch(temp,currentIndex);
        }
    }

    private void onBatch(List<Map<String,?>> batch,long lastIndex){
        batchImportHandler.onBatch(batch,lastIndex);
    }

    @Override
    public void accept(Map<String,?> t) {
        if(temp.size()==batchSize){
            onBatch(temp,currentIndex);
            temp=new ArrayList();
        }
        temp.add(t);
        currentIndex++;
    }
}
